package cn.service.productService.ProductServiceImpl;

import cn.service.productService.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.productEntity.Brand;
import entity.Result;
import mapper.productMapper.BrandMapper;
import mapper.productMapper.PmsProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 朱颢阳
 * @description
 * @time 2024/8/19 19:55
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    BrandMapper mapper;
    @Resource
    PmsProductMapper pmsProductMapper;
    @Override
    public Result selsectAll(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Brand> page= (Page<Brand>) mapper.selectAll();
        System.out.println(page.getTotal());
        return Result.success((int)page.getTotal(),page.getResult());
    }

    @Override
    public Result selectByKey(String key,Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Brand> page= (Page<Brand>) mapper.selectByKey(key);
        System.out.println(page.getTotal());
        return Result.success((int)page.getTotal(),page.getResult());
    }

    @Override
    public Result updatemanu(Integer id, Integer manu) {
        if (mapper.updatemanu(id, manu)>0){
            return Result.success("更新成功");
        }else{
            return Result.failed(200,"更新失败！");
        }
    }

    @Override
    public Result updateshow(Integer id, Integer show) {
        if (mapper.updateshow(id, show)>0){
            return Result.success("更新成功");
        }else{
            return Result.failed(500,"更新失败！");
        }
    }

    @Override
    public Result deletebrand(Integer id) {
        if (pmsProductMapper.selectBrandProduct(id)>0){
            return Result.failed(500,"该字段还有商品存在！删除失败！");
        }else {
            if (mapper.deleteByPrimaryKey(id)>0){
                return Result.success("删除成功");
            }else{
                return Result.failed(500,"删除失败！");
            }
        }
    }

    @Override
    public Result batchdelete(List<Brand> brands) {
        for (Brand brand : brands) {
            if (pmsProductMapper.selectBrandProduct(brand.getBrand_id())>0){
                return Result.failed(500,"该字段还有商品存在！删除失败！编号为"+brand.getBrand_id());
            }else {
                if (mapper.deleteByPrimaryKey(brand.getBrand_id())<1){
                    return Result.failed(500,"删除失败！");
                }
            }
        }
        return Result.success(200,"删除成功！");
    }

    @Override
    public Result insertbrand(Brand brand) {
        if(mapper.insert(brand)>0){
            return Result.success("添加成功");
        }else{
            return Result.failed(500,"添加失败！");
        }
    }

    @Override
    public Result updatabrand(Brand brand) {
        if(mapper.updateByPrimaryKeySelective(brand)>0){
            return Result.success("修改成功");
        }else{
            return Result.failed(500,"修改失败！");
        }
    }

    @Override
    public Result getbrandoption() {
        return Result.success(200,mapper.getbrandoption());
    }

}
